package com.csi.utils;

import lombok.SneakyThrows;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

        public static String DATE_PATTERN3 = "yyyy-MM";

        /**
         * 日期时间格式yyyy-MM-dd HH:mm:ss
         */
        public static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";


        /**
         * 获取默认时间格式: yyyy-MM-dd HH:mm:ss
         */
        public static DateTimeFormatter DEFAULT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss+08:00");
        public static DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        /**
         * 构造函数
         */
    public DateUtils() {
    }

    /**
         * Date转LocalDateTime
         *
         * @param date Date对象
         * @return
         */
        public static LocalDateTime dateToLocalDateTime(Date date) {
            return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        }

        /**
         * LocalDateTime转换为Date
         *
         * @param dateTime LocalDateTime对象
         * @return
         */
        public static Date localDateTimeToDate(LocalDateTime dateTime) {
            return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        }

        /**
         * 格式化时间-默认yyyy-MM-dd HH:mm:ss格式
         *
         * @param dateTime LocalDateTime对象
         * @return
         */
        public static String formatLocalDateTime(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime, DATE_TIME_PATTERN);
        }

        /**
         * 格式化时间为字符串-默认yyyy-MM-dd 格式
         *
         * @param date LocalDate对象
         * @return
         */
        public static String formatLocalDateToStr(LocalDate date, String pattern) {
            if (date == null) {
                return "";
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return date.format(formatter);
        }

        /**
         * String 转时间-默认yyyy-MM-dd格式
         *
         * @param timeStr 字符串
         * @return
         */
        public static LocalDate parseStrToLocalDate(String timeStr) {
            if (timeStr == null || timeStr.isEmpty()) {
                return null;
            }
            return LocalDate.parse(timeStr, DEFAULT_DATE_FORMATTER);
        }

        /**
         * LocalDateTime
         *
         * @param localDateTime
         * @return yyyy-MM-dd
         */
        public static String parseLocalDateTimeToString(LocalDateTime localDateTime) {
            return localDateTime.format(DEFAULT_DATETIME_FORMATTER);
        }

        /**
         * 按pattern格式化时间-默认yyyy-MM-dd HH:mm:ss格式
         *
         * @param dateTime LocalDateTime对象
         * @param pattern  要格式化的字符串
         * @return
         */
        public static String formatLocalDateTime(LocalDateTime dateTime, String pattern) {
            if (dateTime == null) {
                return "";
            }
            if (pattern == null || pattern.isEmpty()) {
                pattern = DATE_TIME_PATTERN;
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return dateTime.format(formatter);
        }

        /**
         * 获取今天的00:00:00
         *
         * @return
         */
        public static String getDayStart() {
            return getDayStart(LocalDateTime.now());
        }

        /**
         * 获取今天的23:59:59
         *
         * @return
         */
        public static String getDayEnd() {
            return getDayEnd(LocalDateTime.now());
        }

        /**
         * 获取某天的00:00:00
         *
         * @param dateTime
         * @return
         */
        public static String getDayStart(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime.with(LocalTime.MIN));
        }

        /**
         * 获取某天的23:59:59
         *
         * @param dateTime
         * @return
         */
        public static String getDayEnd(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime.with(LocalTime.MAX));
        }

        /**
         * 获取本月第一天的00:00:00
         *
         * @return
         */
        public static String getFirstDayOfMonth() {
            return getFirstDayOfMonth(LocalDateTime.now());
        }

        /**
         * 获取本月最后一天的23:59:59
         *
         * @return
         */
        public static String getLastDayOfMonth() {
            return getLastDayOfMonth(LocalDateTime.now());
        }

        /**
         * 获取某月第一天的00:00:00
         *
         * @param dateTime LocalDateTime对象
         * @return
         */
        public static String getFirstDayOfMonth(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN));
        }

        /**
         * 获取某月最后一天的23:59:59
         *
         * @param dateTime LocalDateTime对象
         * @return
         */
        public static String getLastDayOfMonth(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX));
        }

        @SneakyThrows
        public static Date strToDate(String time) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
            return formatter.parse(time);
        }

        /**
         * 将字符串转日期成Long类型的时间戳，格式为：yyyy-MM-dd HH:mm:ss
         */
        public static Time StringToTime(String time){
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            Date d = new Date();
            try {
                d = format.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return  new Time(d.getTime());

        }

        public static Long timeToLong(String time) {
            DateTimeFormatter ftf = DateTimeFormatter.ofPattern("HH:mm:ss");
            LocalDateTime parse = LocalDateTime.parse(time, ftf);
            return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }


        /**
         * 计算 两个时间相差的季度数
         * @param startDate 2017-02-31
         * @param endDate  2017-03-01
         * @return
         */
        public static int differQuarter(String startDate,String endDate){
            int a =0;
            String[] ss= startDate.split("-");
            String[] ee= endDate.split("-");
            String sy =ss[0];
            String sm=ss[1];
            String ey=ee[0];
            String em = ee[1];
            int syi=Integer.parseInt(sy);
            int smi=Integer.parseInt(sm);
            int eyi=Integer.parseInt(ey);
            int emi=Integer.parseInt(em);
            a= ((eyi-syi)*12 +(emi-1))/3-(smi-1)/3+1;
            return a;
        }


        /**
         * 测试
         *
         * @param args
         */
        public static void main(String[] args) {
        System.out.println(getDayStart());
        System.out.println(getDayEnd());
        System.out.println(getFirstDayOfMonth());
        System.out.println(getLastDayOfMonth());
            System.out.println(formatLocalDateToStr(LocalDate.now(), DATE_PATTERN3));
        System.out.println(parseStrToLocalDate("2019-12-12"));
        System.out.println(LocalDate.now().toString().substring(0, 7));
        }


    /**
     * 转成MySQL的date
     * @param d
     * @return
     */
    public java.sql.Date toMysql(java.util.Date d) {
        if (null == d)
            return null;
        return new java.sql.Date(d.getTime());
    }

    /**
     * 转成util的Date
     * @param t
     * @return
     */
     public java.util.Date toDate(java.sql.Date t) {
        if (null == t)
            return null;
        return new java.util.Date(t.getTime());
    }



}
